package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Result;
import com.example.demo.entity.Record;
import com.example.demo.mapper.QuestionMapper;
import com.example.demo.mapper.RecordMapper;
import com.example.demo.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Api(tags = "记录访问接口")
@RestController
@CrossOrigin
@RequestMapping("/api/record")
public class RecordController {
    @Autowired
    private RecordMapper recordmapper;
    @Autowired
    private UserMapper usermapper;
    @Autowired
    private QuestionMapper questionmapper;

    @ApiOperation(value = "获取所有记录信息")
    @GetMapping("/info")
    public Result<?> selectAll(){
        List list = recordmapper.selectList(null);
        if(!list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "找不到记录信息！");
        }
    }

    @ApiOperation(value = "按页数获取所有记录信息")
    @GetMapping("/info/page")
    public Result<?> selectAllByPage(@RequestParam(defaultValue = "1") int num,
                                     @RequestParam(defaultValue = "10") int size,
                                     @RequestParam(defaultValue = "") String str){
        LambdaQueryWrapper<Record> wrapper = Wrappers.<Record>lambdaQuery();
        if (!Objects.equals(str, "")){
            wrapper.like(Record::getUid, str).or().like(Record::getId, str).or().like(Record::getQid, str);
        }

        Page page = recordmapper.selectPage(new Page<>(num, size), wrapper);

        if(!page.getRecords().isEmpty()) {
            return Result.success(page);
        } else {
            return Result.error("404", "找不到记录信息！");
        }
    }

    @ApiOperation(value = "根据记录id获取记录信息")
    @GetMapping("/info/byid")
    public Result<?> selectById(@RequestParam int id){
        if(String.valueOf(id).equals("")){
            return Result.error("401", "参数不能为空！");
        }
        Record record = recordmapper.selectById(id);
        if(record != null) {
            return Result.success(record);
        } else {
            return Result.error("404", "找不到记录信息！");
        }
    }

    @ApiOperation(value = "根据用户id获取记录信息")
    @GetMapping("/info/byuid")
    public Result<?> selectByUid(@RequestParam int uid){
        if(String.valueOf(uid).equals("")){
            return Result.error("401", "参数不能为空！");
        }
        List list = recordmapper.selectList(Wrappers.<Record>lambdaQuery().eq(Record::getUid, uid));
        if(!list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "找不到记录信息！");
        }
    }

    @ApiOperation(value = "获取全部记录数量")
    @GetMapping("/info/count")
    public Result<?> countByid(){
        int count = recordmapper.selectCount(Wrappers.<Record>lambdaQuery());
        return Result.success(count);
    }

    @ApiOperation(value = "根据用户id获取记录数量")
    @GetMapping("/info/count/byuid")
    // 后端请求地址
    public Result<?> countByUid(@RequestParam int uid){
        if(String.valueOf(uid).equals("")){ // 判断参数是否为空
            return Result.error("401", "参数不能为空！");
            // 返回错误信息
        }
        int [] count = new int[2]; // 新建数组
        count[0] = recordmapper.selectCount(Wrappers.<Record>lambdaQuery()
                .eq(Record::getUid, uid));
        // 计算答题数
        count[1] = recordmapper.selectCount(Wrappers.<Record>lambdaQuery()
                .eq(Record::getUid, uid).eq(Record::getRes, 1));
        // 计算正确答题数
        return Result.success(count);
        // 返回信息
    }

    @ApiOperation(value = "根据用户id获取记录数量")
    @GetMapping("/info/count/byqid")
    public Result<?> countByQid(@RequestParam int qid){
        if(String.valueOf(qid).equals("")){
            return Result.error("401", "参数不能为空！");
        }
        int [] count = new int[2];
        count[0] = recordmapper.selectCount(Wrappers.<Record>lambdaQuery().eq(Record::getQid, qid));
        count[1] = recordmapper.selectCount(Wrappers.<Record>lambdaQuery().eq(Record::getQid, qid).eq(Record::getRes, 1));
        return Result.success(count);
    }

    @ApiOperation(value = "根据题目id获取记录信息")
    @GetMapping("/info/byqid")
    public Result<?> selectByQid(@RequestParam int qid){
        if(String.valueOf(qid).equals("")){
            return Result.error("401", "参数不能为空！");
        }
        List list = recordmapper.selectList(Wrappers.<Record>lambdaQuery().eq(Record::getQid, qid));
        if(!list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "找不到记录信息！");
        }
    }

    @ApiOperation(value = "添加记录信息")
    @PostMapping("")
    public Result<?> insert(Record record){
        if(usermapper.selectById(record.getUid()) == null) {
            return Result.error("404", "记录的用户id不存在！");
        }
        if((questionmapper.selectById(record.getQid())) == null) {
            return Result.error("404", "记录的题目id不存在！");
        }

        LambdaQueryWrapper<Record> wrapper = Wrappers.<Record>lambdaQuery();
        wrapper.eq(Record::getUid, record.getUid()).eq(Record::getQid, record.getQid());

        Record record1 = recordmapper.selectOne(wrapper);

        if(record1 != null) {
            record1.setRes(record.getRes());
            update(record1);
            return Result.success(1);
        }

        boolean bool = (recordmapper.insert(record) > 0);
        if(bool) {
            return Result.success();
        } else {
            return Result.error("-1", "添加记录失败！");
        }
    }

    @ApiOperation(value = "根据记录id删除记录信息")
    @DeleteMapping("")
    public Result<?> delete(@RequestParam int id){
        if(String.valueOf(id).equals("")){
            return Result.error("401", "参数不能为空！");
        }
        boolean bool = (recordmapper.deleteById(id) > 0);
        if(bool) {
            return Result.success();
        } else {
            return Result.error("-1", "删除记录失败！");
        }
    }

    @ApiOperation(value = "根据记录id更新记录信息")
    @PutMapping("")
    public Result<?> update(Record record){
        if(record.getId() == 0) {
            return Result.error("401", "参数不能为空！");
        }

        if(usermapper.selectById(record.getUid()) == null) {
            return Result.error("404", "记录的用户id不存在！");
        }
        if((questionmapper.selectById(record.getQid())) == null) {
            return Result.error("404", "记录的题目id不存在！");
        }

        boolean bool = (recordmapper.updateById(record) > 0);
        if(bool) {
            return Result.success();
        } else {
            return Result.error("-1", "更新记录失败！");
        }
    }
}
